﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TinyCinema.Data;
using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;

namespace TinyCinema.Bussiness.Movie
{

    public class Movies:DependencyObject
    {
        public static DependencyProperty lstMovieProperty = DependencyProperty.Register("lstMovie", typeof(ObservableCollection<Data.Movie>), typeof(Movie.Movies));
        public ObservableCollection<Data.Movie> lstMovie
        {
            get { return (ObservableCollection<Data.Movie>)GetValue(lstMovieProperty); }
            set
            {
                SetValue(lstMovieProperty, value);
            }
        }

        public static DependencyProperty CurrentViewProperty = DependencyProperty.Register("CurrentView", typeof(Data.Movie), typeof(Movie.Movies));
        public Data.Movie CurrentView
        {
            get { return (Data.Movie)GetValue(CurrentViewProperty); }
            set 
            { 
                SetValue(CurrentViewProperty, value);
                if (value == null) return;
                if ((value as Data.Movie).ID == null) IsEditable = true;
                else IsEditable = false;
            }
        }
        public static DependencyProperty CurrentViewIndexProperty = DependencyProperty.Register("CurrentViewIndex", typeof(int), typeof(Movie.Movies));
        public int CurrentViewIndex
        {
            get { return (int)GetValue(CurrentViewIndexProperty); }
            set {
                if (value < 1 || value > lstMovie.Count) return;
                try { CurrentView = lstMovie[value - 1]; }
                catch { return; }
                SetValue(CurrentViewIndexProperty, value);                
            }
        }

        public static DependencyProperty IsEditableProperty = DependencyProperty.Register("IsEditable", typeof(bool), typeof(Movie.Movies));
        public bool IsEditable
        {
            get { return (bool)GetValue(IsEditableProperty); }
            set { 
                SetValue(IsEditableProperty, value); 
            }
        }
        public static DependencyProperty strSearchProperty = DependencyProperty.Register("strSearch", typeof(string), typeof(Movie.Movies));
        public string strSearch
        {
            get { return (string)GetValue(strSearchProperty); }
            set
            {
                SetValue(strSearchProperty, value);
            }
        }
        public static DependencyProperty IsSearchingProperty = DependencyProperty.Register("IsSearching", typeof(bool), typeof(Movie.Movies));
        public bool IsSearching
        {
            get { return (bool)GetValue(IsSearchingProperty); }
            set
            {
                SetValue(IsSearchingProperty, value);
            }
        }
        public static DependencyProperty IsViewModeEnableProperty = DependencyProperty.Register("IsViewModeEnable", typeof(bool), typeof(Movie.Movies));
        public bool IsViewModeEnable
        {
            get { return (bool)GetValue(IsViewModeEnableProperty); }
            set { 
                SetValue(IsViewModeEnableProperty, value); 
            }
        }
        public static DependencyProperty SearchFilterProperty = DependencyProperty.Register("SearchFilter", typeof(ObservableCollection<String>), typeof(Movie.Movies));
        public ObservableCollection<String> SearchFilter
        {
            get { return (ObservableCollection<String>)GetValue(SearchFilterProperty); }
            set { SetValue(SearchFilterProperty, value); }
        }
        public static DependencyProperty SearchFilterIndexProperty = DependencyProperty.Register("SearchFilterIndex", typeof(int), typeof(Movie.Movies));
        public int SearchFilterIndex
        {
            get { return (int)GetValue(SearchFilterIndexProperty); }
            set { SetValue(SearchFilterIndexProperty, value); }
        }
        public ICommand ChooseMovieCommand { get; set; }
        public ICommand CreateCommand { get; set; }
        public ICommand EditCommand { get; set; }
        public ICommand SaveCommand { get; set; }
        public ICommand BackCommand { get; set; }
        public ICommand AddPosterCommand { get; set; }
        public ICommand DeleteCommand { get; set; }
        public ICommand AddActorCommand { get; set; }
        public ICommand AddGenreCommand { get; set; }
        public ICommand RemoveActorCommand { get; set; }
        public ICommand RemoveGenreCommand { get; set; }
        public ICommand PreviousCommand { get; set; }
        public ICommand NextCommand { get; set; }
        public ICommand GoToPageCommand { get; set; }
        public ICommand SearchCommand { get; set; }
        public ICommand SearchCancelCommand { get; set; }

        bool IsCreating;
        public Messenger Messenger { get; set; }
        public Movies()
        {
            Messenger = new Bussiness.Messenger();
            lstMovie = Data.Movie.GetAllMovie();            
            IsEditable = true; // Disable nút Edit
            IsViewModeEnable = true;
            CurrentView = new Data.Movie();
            ChooseMovieCommand = new RelayCommand<Data.Movie>(
                (s) => true,
                (s) =>
                {
                    CurrentViewIndex = lstMovie.IndexOf(s) + 1;
                    CurrentView = s;
                    IsEditable = false;
                    IsViewModeEnable = false;
                }
            );
            CreateCommand = new RelayCommand<Data.Movie>(
                (s) => IsViewModeEnable,
                (s) =>
                {
                    CurrentView = new Data.Movie();
                    IsEditable = true;
                    IsViewModeEnable = false;
                    IsCreating = true;
                }
            );
            EditCommand = new RelayCommand<Data.Movie>(
                (s) => !IsEditable&&!IsViewModeEnable,
                (s) =>
                {
                    IsEditable = true;
                }
            );
            SaveCommand = new RelayCommand<Data.Movie>(
                (s) => true,
                (s) =>
                {
                    if (CurrentView.ID == null) lstMovie.Add(CurrentView);  
                    if (IsCreating)
                    {
                        CurrentViewIndex = lstMovie.IndexOf(CurrentView) + 1;
                        IsCreating = false;
                    }
                    CurrentView.Save();
                    IsEditable = false;
                }
            );
            BackCommand = new RelayCommand<Data.Movie>(
                (s) => true,
                (s) =>
                {
                    if (IsEditable)
                    {
                        var message1 = new Message.BackConfimation();
                        Messenger.Send(message1);
                        if (message1.Result == MessageBoxResult.Yes)
                        {
                            Messenger.Send(new Message.BackConfimationOK());
                            if (CurrentView.ID == null) return;
                            lstMovie[CurrentViewIndex - 1] = new Data.Movie(CurrentView.ID);
                            CurrentView = lstMovie[CurrentViewIndex - 1];
                            IsViewModeEnable = true;
                            IsEditable = true;
                            
                        }
                    }
                    else
                    {
                        IsViewModeEnable = true;
                        IsEditable = true;
                        Messenger.Send(new Message.BackConfimationOK());
                    }
                }
            );
            AddPosterCommand = new RelayCommand<object>(
                (s) => true,
                (s) =>
                {
                    CurrentView.AddPoster();
                }
            );
            AddActorCommand = new RelayCommand<object>(
                (s) => true,
                (s) =>
                {
                    CurrentView.Actors.Add(new Actor());
                }
            );
            RemoveActorCommand = new RelayCommand<Actor>(
                (s) => true,
                (s) =>
                {
                    CurrentView.Actors.Remove(s);
                }
            );
            AddGenreCommand = new RelayCommand<Genre>(
                (s) => true,
                (s) =>
                {
                    CurrentView.Genres.Add(new Genre());
                }
            );
            RemoveGenreCommand = new RelayCommand<Genre>(
                (s) => true,
                (s) =>
                {
                    CurrentView.Genres.Remove(s);
                }
            );
            DeleteCommand = new RelayCommand<Data.Movie>(
                (s) => !IsViewModeEnable,
                (s) =>
                {
                    try
                    {
                        var message = new Message.DeleteConfirmation();
                        Messenger.Send(message);
                        if (message.Result == MessageBoxResult.No) return;

                        CurrentView.Delete();
                        lstMovie.Remove(CurrentView);
                        IsViewModeEnable = true;
                        IsEditable = true;
                        Messenger.Send(new Message.BackConfimationOK());
                    }
                    catch
                    {
                        Messenger.Send(new Message.DeleteFailed());
                        Data.Execution.Restore();
                    }
                }
            );

            PreviousCommand = new RelayCommand<Data.Movie>(
                (s) => CurrentViewIndex>1,
                (s) =>
                {
                    --CurrentViewIndex;
                }
            );
            NextCommand = new RelayCommand<Data.Movie>(
                (s) => CurrentViewIndex < lstMovie.Count,
                (s) =>
                {
                    ++CurrentViewIndex;
                }
            );
            GoToPageCommand = new RelayCommand<Data.Movie>(
                (s) => true,
                (s) =>
                {
                    CurrentViewIndex = CurrentViewIndex;
                }
            );
            SearchFilter = new ObservableCollection<string>();            
            SearchFilterIndex = 0;
            SearchCommand = new RelayCommand<String>(
                (s) => true,
                (s) =>
                {
                    if (!IsSearching)
                    {
                        IsSearching = true;
                        return;
                    }
                    switch(SearchFilterIndex)
                    {
                        case 0: lstMovie= Data.Movie.SearchByName(s);
                            break;
                        case 1: lstMovie = Data.Movie.SearchByDirector(s);
                            break;
                        case 2: lstMovie = Data.Movie.SearchByActor(s);
                            break;
                        case 3: lstMovie = Data.Movie.SearchByGenre(s);
                            break;
                    }
                }
            );
            SearchCancelCommand = new RelayCommand<object>(
                (s) => true,
                (s) =>
                {
                    IsSearching = false;
                    lstMovie = Data.Movie.GetAllMovie();
                }
            );
            
  
        }
    }
}
